<?php

namespace Modules\SmallApi\Service;

use HiCommon\Repository\AreasRepository;
use HiCommon\Repository\ShopRepository;

class PublicService
{
    /**
     * 根据城市编码获取城市信息
     */
    public function get_city_infor($city)
    {
        try {
            if (empty($city)) {
                return hello_error('城市名称不能为空');
            }
            $data = AreasRepository::get_city_infor(['name' => $city]);
            if (is_null($data)) {
                return hello_error('数据不存在');
            }
            return hello_success('成功', $data->toarray());
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }

    }

    /**
     * 获取已经开通的城市
     */
    public function get_open_city()
    {
        try {
            $data = ShopRepository::get_open_city();
            if (is_null($data)) {
                return hello_error('数据不存在');
            }
            $arr_data = [];
            foreach ($data as $value)
            {
                $arr_data['city_'.$value['city_name']['id']] = $value;
            }

            return hello_success('成功', ['arr_data'=>$data,'deal_data'=>$arr_data]);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }


    /**
     * 获取省列表
     */
    public function getProvinceList()
    {
        $province = AreasRepository::get_list(['pid' => 1]);
        $list = array();
        foreach ($province as $key => $item) {
            $list[$item['id']] = $item['name'];
        }

        return $list;
    }


    /**
     * 获取列表
     */
    public function getCityAraeList(array $data)
    {
        try {

            if ($data <= 0) {
                return hello_error('城市id不能为空');
            }

            $list = AreasRepository::get_list(['pid' => $data]);

            if (is_null($list)) {
                return hello_error('数据不存在');
            }

            return hello_success('成功', $list);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }

    }











}